# -*- coding: utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json
import time

PAGE_SIZE = 3
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

'''
姚明 281720252@qq.com
'''


class ProcessAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()  # 初始化数据库链接

    def queryProcess(self, page_index):  # process表查询记录

        cur = self.conn.cursor()
        sql = "select * from zzh_process"
        total_count = cur.execute(sql)
        processList = cur.fetchall()
        cur.close()
        self.conn.close()

        if (page_index * PAGE_SIZE) < total_count:
            start = (page_index - 1) * PAGE_SIZE
            end = page_index * PAGE_SIZE
            processListPart = processList[start: end]

            processListJson = Table2Json.process_list_to_json(processListPart)
            return total_count, processListJson
        else:
            start = (page_index - 1) * PAGE_SIZE
            processListPart = processList[start:]
            processListJson = Table2Json.process_list_to_json(processListPart)
            return total_count, processListJson

    def addProcess(self, addProcess):  # process表添加记录
        processSum = addProcess['processSum']  # processSum  流程的总步数
        processCode = addProcess['processCode']  # processCode 流程编号
        processStatus = addProcess['processStatus']  # processStatus  流程状态
        processName = addProcess['processName']

        cur = self.conn.cursor()
        sql = "INSERT INTO zzh_process (process_sum,process_code,process_status,process_name) values (%s,%s,%s,%s)"
        addResultOfDatabase = cur.execute(sql, [processSum, processCode, processStatus, processName])
        self.conn.commit()
        if addResultOfDatabase:  # 插入成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 插入失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def updateProcessByProcessId(self, newUpdateProcess, processId):
        processSum = newUpdateProcess['processSum']
        processCode = newUpdateProcess['processCode']
        processStatus = newUpdateProcess['processStatus']
        processName = newUpdateProcess['processName']
        cur = self.conn.cursor()
        sql = "UPDATE zzh_process SET process_sum=%s,process_code=%s,process_status=%s,process_name=%s WHERE process_id=%s"
        updateResultOfDatabase = cur.execute(sql, [processSum, processCode, processStatus, processName, processId])
        self.conn.commit()
        if updateResultOfDatabase:  # 更新成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 更新失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def deleteProcessById(self, processId):
        cur = self.conn.cursor()
        sql = "DELETE from zzh_process WHERE process_id=%s"
        deleteResultOfDatabase = cur.execute(sql, [processId])
        self.conn.commit()
        if deleteResultOfDatabase:  # 删除成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:  # 删除失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0
